Dim FileName, dFileContents, arrFilename, PathToWIXBinaries, a, Drive

If WScript.Arguments.Count <> 2 Then
	WScript.Echo ("wrong usage - use" & vbNewLine & "Buildx64Config.vbs [*.wxs file] [path to WIX binaries(candle.exe & light.exe)]")
Else
	FileName = WScript.Arguments(0)
	PathToWIXBinaries = WScript.Arguments(1)
	Drive = Left(PathToWIXBinaries,2)
	WorkingFolder = GetWorkingFolder(FileName)
	arrFilename = Split(FileName, ".")
	'Read source text file
	dFileContents = GetFile(FileName)
	'replace all string In the source file
	a = Array("VBScriptCall="&chr(34)&"getIPs"&chr(34),"VBScriptCall="&chr(34)&"getWebsites"&chr(34),"VBScriptCall="&chr(34)&"checkPort"&chr(34),"VBScriptCall="&chr(34)&"checkHostname"&chr(34),"VBScriptCall="&chr(34)&"checkDescription"&chr(34),"VBScriptCall="&chr(34)&"checkForConflictWithExistingWebsite"&chr(34),"VBScriptCall="&chr(34)&"getIISDefaultWebsitePath"&chr(34),"VBScriptCall="&chr(34)&"getIISWebsitePath"&chr(34),"VBScriptCall="&chr(34)&"checkFolderStructure"&chr(34),"VBScriptCall="&chr(34)&"checkInstallDirSet"&chr(34),"VBScriptCall="&chr(34)&"checkIIs"&chr(34),"VBScriptCall="&chr(34)&"setIISWebsiteData"&chr(34),"VBScriptCall="&chr(34)&"checkVirtualDirectoryName"&chr(34),"VBScriptCall="&chr(34)&"checkIUSR"&chr(34),"VBScriptCall="&chr(34)&"checkPHP"&chr(34),"VBScriptCall="&chr(34)&"checkPERL"&chr(34),"VBScriptCall="&chr(34)&"addPerlExtension"&chr(34),"VBScriptCall="&chr(34)&"removePerlExtension"&chr(34),"VBScriptCall="&chr(34)&"checkIIS7AndFastCgi"&chr(34))

	for each entry in a
		'msgbox entry & " : " & entry & " Win64="&chr(34)&"yes"&chr(34)
		newentry = entry & " Win64="&chr(34)&"yes"&chr(34)
		dFileContents = replace(dFileContents, entry, newentry, 1, -1, 0)
	next 

	dFileContents = replace(dFileContents, "Languages=""1033""", "Languages=""1033"" Platform=""x64""", 1, -1, 0)

	WriteFile arrFilename(0) & "_x64.wxs", dFileContents
	Set objShell = CreateObject("Wscript.Shell")
	objShell.Run("cmd.exe /K Build.bat "& Drive & " " & chr(34)&PathToWIXBinaries &chr(34) & " " &chr(34)& WorkingFolder & arrFilename(0) &".wxs" &chr(34) & " " &chr(34)& WorkingFolder & arrFilename(0) &".wixobj" &chr(34)& " " &chr(34)&WorkingFolder & arrFilename(0) &".msi"&chr(34))
	objShell.Run("cmd.exe /K Build.bat "& Drive & " " & chr(34)&PathToWIXBinaries &chr(34) & " " &chr(34)& WorkingFolder & arrFilename(0) &"_x64.wxs" &chr(34) & " " &chr(34)& WorkingFolder & arrFilename(0) &"_x64.wixobj" &chr(34)& " " &chr(34)&WorkingFolder & arrFilename(0) &"_x64.msi"&chr(34))
end if

'Read text file
function GetFile(FileName)
  If FileName<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(FileName)
      GetFile = FileStream.ReadAll
	  FS = nothing
  End If
End Function


function GetWorkingFolder(FileName)
  If FileName<>"" Then
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
      on error resume Next
	     GetWorkingFolder = replace(fso.GetAbsolutePathName(FileName),FileName,"", 1, -1, 0)
		 fso = nothing
  End If
End Function

'Write string As a text file.
function WriteFile(FileName, Contents)
  Dim OutStream, FS
  on error resume Next
  Set FS = CreateObject("Scripting.FileSystemObject")
    Set OutStream = FS.OpenTextFile(FileName, 2, True)
    OutStream.Write Contents
	OutStream.Close
	FS = nothing
End Function